package com.showcai.service.impl;

import com.showcai.dao.ScheduleMapper;
import com.showcai.module.Schedule;
import com.showcai.myRunTime.MyRunTimeExcption;
import com.showcai.result.ReturnCode;
import com.showcai.service.ScheduleService;
import com.showcai.service.ServiceBase;
import org.apache.commons.lang3.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;

import java.util.Date;
import java.util.List;

/**
 * Created by Baggio on 2018-8-23.
 */
public class ScheduleServiceImpl extends ServiceBase implements ScheduleService {
    @Autowired
    ScheduleMapper scheduleMapper;


    private void insert(Schedule schedule) {
        scheduleMapper.insertSchedule(schedule);
        //TODO 通知手表更新

    }

    private void update(Schedule schedule) {
        scheduleMapper.updateScheduleByPrimaryKeySelective(schedule);
//TODO 通知手表更新，,type=1表示是日程管理的更新
    }

    @Override
    public List<Schedule> getScheduleListByDevicId(String devicId) throws MyRunTimeExcption {
        if (StringUtils.isBlank(devicId)) {
            throw new MyRunTimeExcption(ReturnCode.DEVIC_ID_IS_NULL);
        }
        return scheduleMapper.selectScheduleListByDevicId(devicId);
    }

    @Override
    public void modifySchedule(Schedule schedule) throws MyRunTimeExcption {

        if (schedule.getTid() == null) {
            schedule.setCreateTime(new Date());
            schedule.setStatus(Schedule.STATUS_VALID);
            insert(schedule);
            return;
        }
        update(schedule);

    }

    @Override
    public void delScheduleById(String devicId, Long id) throws MyRunTimeExcption {
        Schedule schedule = scheduleMapper.selectScheduleListById(id);
        if (schedule == null) {
            throw new MyRunTimeExcption(ReturnCode.SCHEDULE_IS_INEXISTENCE);
        }
        schedule.setStatus(Schedule.STATUS_UNVALID);
        scheduleMapper.updateScheduleById(schedule);

    }
}
